package com.neuedu.yyzx.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.yyzx.common.Ret;
import com.neuedu.yyzx.po.Food;
import com.neuedu.yyzx.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/food")
public class FoodController {

    @Autowired
    private FoodService foodService;

    @PostMapping("/add")
    public Ret<Boolean> addFood(@RequestBody Food food) {
//        System.out.println("sadfsafdsfndsfjhdsfhd\n");
        System.out.println(food);
//        foodService.saveOrUpdate(food);
//        return Ret.ok( foodService.addFood(food));
        return Ret.ok( foodService.saveOrUpdate(food));
    }

    @DeleteMapping("/delete/{foodId}")
    public Ret<Boolean> deleteFood(@PathVariable Integer foodId) {

//        return Ret.ok(foodService.deleteFood(foodId));
        return Ret.ok(foodService.removeById(foodId));
    }

    @PutMapping("/update")
    public Ret<Boolean> updateFood(@RequestBody Food food) {
//        return Ret.ok(foodService.updateFood(food));
        return Ret.ok(foodService.saveOrUpdate(food));
    }
    // http://localhost:8080/food/list

    @GetMapping("/list")
    public Ret<Page<Food>> listFoods(Page page ,
                                     @RequestParam(required = false) Integer isHalal,
                                     @RequestParam(required = false) String foodName
                                     /*@RequestParam(defaultValue = "1") Integer pageNum,
                                     @RequestParam(defaultValue = "10") Integer pageSize*/) {
        // 根据 isHalal、foodName、pageNum 和 pageSize 参数调用服务层方法
        return Ret.ok(foodService.listFoods(page, isHalal, foodName));
    }

    @GetMapping("/get/{foodId}")
    public Food getFoodById(@PathVariable Integer foodId) {
//        return foodService.getFoodById(foodId);
        return foodService.getById(foodId);
    }
}